//
//  MJ_DetailsCell.m
//  NewClothesForEveryDay
//
//  Created by dllo on 15/7/15.
//  Copyright (c) 2015年 蓝天. All rights reserved.
//

#import "MJ_DetailsCell.h"
#import "BaseHeader.h"
#import "UIImageView+WebCache.h"

@implementation MJ_DetailsCell

- (void)dealloc
{
    [_picModel release];
    [_myImageView release];
    [_myLabel release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.myImageView = [[[UIImageView alloc] init] autorelease];
        self.myImageView.backgroundColor = [UIColor redColor];//
        self.myLabel = [[[UILabel alloc] init] autorelease];
        self.myLabel.backgroundColor = [UIColor whiteColor];//
        [self.contentView addSubview:self.myImageView];
        [self.contentView addSubview:self.myLabel];
    }
    return self;
}

- (void)layoutSubviews
{
    self.myImageView.frame = CGRectMake(0, 0, ScreenWidth, [self heightWithPic]);
    self.myLabel.font = [UIFont systemFontOfSize:12];
    self.myLabel.frame = CGRectMake(0, self.myImageView.frame.size.height - [self heightWithText], self.myImageView.frame.size.width, [self heightWithText]);
    
}

- (void)setPicModel:(MJTwoPicModel *)picModel
{
    if (_picModel != picModel) {
        [_picModel release];
        _picModel = [picModel retain];
    }
    NSString *url = [NSString stringWithFormat:@"http://roseimg.b0.upaiyun.com/%@", picModel.url];
    [self.myImageView setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"a"]];
//      [self.myImageView setContentMode:UIViewContentModeScaleToFill];
    
    self.myLabel.text = picModel.comment;
    self.myLabel.numberOfLines = 0;
    /**
     *  判断是否有Label
     */
    if ([picModel.comment isEqualToString:@""]) {
        [self.myLabel removeFromSuperview];
    }
    
}

- (CGFloat)heightWithPic
{
    return [self.picHeight floatValue] * ScreenWidth / [self.picWidth floatValue];
}

/**
 *  自适应
 */
- (CGFloat)heightWithText
{
    //保存文本字体的大小  和Label设置的文本字体大小一致
    NSDictionary *dic = @{NSFontAttributeName: [UIFont systemFontOfSize:12]};
    //预设一个尺寸大小 文本最大不超过这个尺寸
    // 375 是 自己上面设定的Label的宽度
    CGSize size = CGSizeMake(ScreenWidth - 10, 2000);
    //根据文本内容获得一个CGrect
    //参数一 尺寸范围
    //参数二 按照什么方式获取rect
    //参数三 文本属性
    CGRect rect = [self.picModel.comment boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    //返回得到的高度
    return rect.size.height;
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
